package com.ruoyi.project.oms.paeaAuAuditItem.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.project.er.domain.PaeaErError;
import com.ruoyi.project.er.service.IPaeaErErrorService;
import com.ruoyi.project.oms.paeaAuAuditItem.enums.AuditItemState;
import com.ruoyi.project.oms.paeaAuBatch.domain.PaeaAuBatch;
import com.ruoyi.project.oms.paeaAuBatch.model.PaeaAuBatchRequest;
import com.ruoyi.project.oms.paeaAuBatch.service.IPaeaAuBatchService;
import com.ruoyi.project.oms.paeaAuOperAuditor.domain.PaeaAuOperAuditor;
import com.ruoyi.project.oms.paeaAuOperAuditor.service.IPaeaAuOperAuditorService;
import com.ruoyi.project.sdi.sdiPaeaArc.domain.SdiPaeaArc;
import com.ruoyi.project.sdi.sdiPaeaArc.service.ISdiPaeaArcService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.oms.paeaAuAuditItem.domain.PaeaAuAuditItem;
import com.ruoyi.project.oms.paeaAuAuditItem.service.IPaeaAuAuditItemService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import javax.servlet.http.HttpServletRequest;

/**
 * 重点监督Controller
 *
 * @author px
 * @date 2020-03-22
 */
@Controller
@RequestMapping("/oms/paeaAuAuditItem")
public class PaeaAuAuditItemController extends BaseController
{
    private String prefix = "oms/paeaAuAuditItem";

    @Autowired
    private IPaeaAuAuditItemService paeaAuAuditItemService;
    @Autowired
    private IPaeaAuBatchService paeaAuBatchService;
    @Autowired
    private IPaeaErErrorService paeaErErrorService;
    @Autowired
    private ISdiPaeaArcService sdiPaeaArcService;
    @Autowired
    private IPaeaAuOperAuditorService paeaAuOperAuditorService;


    @RequiresPermissions("oms:paeaAuAuditItem:view")
    @GetMapping()
    public String paeaAuAuditItem()
    {
        return prefix + "/auditLoadList";
    }

    /**
     * 跳转监督页面
     * @return
     */
    @GetMapping("/auditBatchDetail")
    public String auditBatchDetail(ModelMap mmap, @RequestParam(name = "queryFlag") String queryFlag,
                                   @RequestParam(name = "isViewOnly") String isViewOnly,
                                   @RequestParam(name = "arcId") String arcId)
    {
        mmap.put("queryFlag",queryFlag);
        mmap.put("isViewOnly",isViewOnly);
        mmap.put("arcId",arcId);
        return prefix + "/auditBatchDetail";
    }

    /**
     * 跳转质量检查页面
     * @return
     */
    @GetMapping("/reAuditItemInfo")
    public String reAuditItemInfo(ModelMap mmap,@RequestParam(name = "batchId") String batchId,
                                  @RequestParam(name = "beginDate") String beginDate,
                                  @RequestParam(name = "endDate") String endDate,
                                  @RequestParam(name = "orgId") String orgId,
                                  @RequestParam(name = "state") String state,
                                  @RequestParam(name = "batchType") String batchType,
                                  @RequestParam(name = "auditorId") String auditorId)
    {
        //PaeaAuBatch paeaAuBatch = paeaAuBatchService.getById(batchId);
        mmap.put("batchId",batchId);
        mmap.put("beginDateInput",beginDate);
        mmap.put("endDateInput",endDate);
        mmap.put("orgIdInput",orgId);
        mmap.put("stateInput",state);
        mmap.put("batchTypeInput",batchType);
        mmap.put("auditorIdInput",auditorId);
        return prefix + "/reAuditItemInfo";
    }
    /**
     * 查询重点监督列表
     */
    @RequiresPermissions("oms:paeaAuAuditItem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaAuAuditItem paeaAuAuditItem)
    {
        startPage();
        List<PaeaAuAuditItem> list = paeaAuAuditItemService.list(new QueryWrapper<>(paeaAuAuditItem));
        return getDataTable(list);
    }

    /**
     * 导出重点监督列表
     */
    @RequiresPermissions("oms:paeaAuAuditItem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaAuAuditItem paeaAuAuditItem)
    {
        List<PaeaAuAuditItem> list = paeaAuAuditItemService.list(new QueryWrapper<>(paeaAuAuditItem));
        ExcelUtil<PaeaAuAuditItem> util = new ExcelUtil<PaeaAuAuditItem>(PaeaAuAuditItem.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 新增重点监督
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存重点监督
     */
    @RequiresPermissions("oms:paeaAuAuditItem:add")
    @Log(title = "新增保存重点监督", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaAuAuditItem paeaAuAuditItem)
    {
        return toAjax(paeaAuAuditItemService.saveOrUpdate(paeaAuAuditItem));
    }

    /**
     * 修改重点监督
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaAuAuditItem paeaAuAuditItem = paeaAuAuditItemService.getById(id);
        mmap.put("paeaAuAuditItem", paeaAuAuditItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存重点监督
     */
    @RequiresPermissions("oms:paeaAuAuditItem:edit")
    @Log(title = "修改保存重点监督", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaAuAuditItem paeaAuAuditItem)
    {
        return toAjax(paeaAuAuditItemService.saveOrUpdate(paeaAuAuditItem));
    }

    /**
     * 删除重点监督
     */
    @RequiresPermissions("oms:paeaAuAuditItem:remove")
    @Log(title = "删除重点监督", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaAuAuditItemService.removeByIds(Arrays.asList(ids.split(","))));
    }


    @RequiresPermissions("oms:paeaAuBatch:auditWrapperTable")
    @PostMapping("/auditWrapperTable")
    @ResponseBody
    public TableDataInfo auditWrapperTable(PaeaAuAuditItem paeaAuAuditItem){
        startPage();
        String batchId = paeaAuAuditItem.getBatchId();
        List<PaeaAuAuditItem> paeaAuAuditItemList = new ArrayList<PaeaAuAuditItem>();
        if(batchId != null && !"".equals(batchId)){
            paeaAuAuditItemList = paeaAuAuditItemService.queryItemForBatchId(batchId);
        }
        return getDataTable(paeaAuAuditItemList);
    }

    /**
     * 通过流水查询影像 by itemId
     * @param paeaAuAuditItem
     * @return AjaxResult
     */
    @RequiresPermissions("oms:paeaAuAuditItem:querySdiImagesByTrade")
    @PostMapping("/querySdiImagesByTrade")
    @ResponseBody
    public AjaxResult querySdiImagesByTrade(PaeaAuAuditItem paeaAuAuditItem) {
        String itemId = paeaAuAuditItem.getId();
        if(itemId == null || "".equals(itemId)){
            AjaxResult.warn("ID 为空");
        }
        String taskString = paeaAuAuditItemService.querySdiImagesByTrade(itemId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("taskString",taskString);
        return ajax;
    }

    /**
     * 通过流水查询影像 by batchId
     * @param paeaAuAuditItem
     * @return AjaxResult
     */
    @RequiresPermissions("oms:paeaAuAuditItem:querySdiImagesWithTrade")
    @PostMapping("/querySdiImagesWithTrade")
    @ResponseBody
    public AjaxResult querySdiImagesWithTrade(PaeaAuAuditItem paeaAuAuditItem) {
        String batchId = paeaAuAuditItem.getBatchId();
        String itemId = "";
        List<PaeaAuAuditItem> itemList = paeaAuAuditItemService.list(new QueryWrapper<PaeaAuAuditItem>()
                .eq(batchId !=null,"batch_id",batchId)
                .eq("re_au_flag",1)
                .orderByAsc("vch_no"));
        if(itemList != null && itemList.size()>0){
            itemId = itemList.get(0).getId();
            String taskString = paeaAuAuditItemService.querySdiImagesByTrade(itemId);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("taskString",taskString);
            return ajax;
        }else{
            return AjaxResult.warn("该批次下流水都已检查");
        }

    }
    /**
     * 验证是否已做过正常确认
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:checkAuAudit")
    @PostMapping("/checkAuAudit")
    @ResponseBody
    public AjaxResult checkAuAudit(PaeaAuAuditItem paeaAuAuditItem) {
        try {
            String id = paeaAuAuditItem.getId();
            PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(id);
            int state = 0;
            if(item != null){
                state = item.getState();
            }
            String status = "";
            if (state == AuditItemState.NORMAL_PASS.getCode()) {
                status = "已正常确认过，不能再次正常确认！";
            }
            if(state == AuditItemState.HAVE_ERROR.getCode()){
                status = "已创建督办，不能进行正常确认！";
            }
            return AjaxResult.success(status);
        } catch (RuntimeException e) {
            return AjaxResult.warn("出现系统错误，请联系管理员");
        }
    }

    /**
     *提交监控项
     * @param paeaAuAuditItem
     * @param request
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:submitAuditItem")
    @PostMapping("/submitAuditItem")
    @ResponseBody
    public AjaxResult submitAuditItem(PaeaAuAuditItem paeaAuAuditItem, HttpServletRequest request) {
        try {
            /**
             * 获取基本信息
             */
            int state = paeaAuAuditItem.getState();
            String id = paeaAuAuditItem.getId();
            String errSourceS = request.getParameter("errSource");
            int errSource = 0;
            if (errSourceS != null) {
                errSource = Integer.valueOf(errSourceS).intValue();
            }
            String batchId = paeaAuAuditItem.getBatchId();
            PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(id);
            PaeaAuBatch paeaAuBatch = paeaAuBatchService.selectPaeaAuBatchById(batchId);
            String submitResult = "提交异常，不存在paeaAuBatch";
            if (null != paeaAuBatch) {
                paeaAuBatch.setAuditorId(getLoginName());
                paeaAuBatch.setFinishDate(null);
                submitResult = paeaAuAuditItemService.submitItem(paeaAuBatch, item, state, errSource, getLoginName());
            }
            return AjaxResult.success(submitResult);
        } catch (RuntimeException e) {
            return AjaxResult.error("提交失败");
        }
    }

    /**
     * 验证对创建督办的监控进行取消监控操作
     * */
    @RequiresPermissions("oms:paeaAuAuditItem:checkCancelError")
    @PostMapping("/checkCancelError")
    @ResponseBody
    public AjaxResult checkCancelError(PaeaAuAuditItem paeaAuAuditItem) {
        try {
            String id = paeaAuAuditItem.getId();
            PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(id);
            java.util.Date trDate = item.getTrDate();
            String tradeId = item.getDsTradeId();
            //PaeaAuAuditItem item = getPaeaAuAuditItemFromSession(request, id, id, trDate);
            int state = 0;
            String erTypeId = "4000";//重点监控
            if(item != null){
                state = item.getState();
            }
            //确认正常的监控
            String returnString = "0|A";
            if(state == AuditItemState.HAVE_ERROR.getCode()){
                PaeaErError pee = null;
                List<PaeaErError> list = paeaErErrorService.list(new QueryWrapper<PaeaErError>()
                        .eq(tradeId!=null && tradeId != "","trade_id",tradeId)
                        .eq(erTypeId != null && erTypeId != "","err_type_id",erTypeId)
                        .eq("err_status",0));//状态为0,已保存未提交
                if(list != null && list.size() > 0){
                    pee = list.get(0);
                }
                if(null == pee)
                {   //已经提交的监控
                    returnString = "1|已提交督办，不能取消监控操作！";
                }
                else
                {   //已保存的监控
                    returnString = "2|" + pee.getId();
                }
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("returnString",returnString);
            return ajax;
        } catch (RuntimeException e) {
            return AjaxResult.error();
        }
    }

    /**
     * 创建督办后 更新paeaAuAuditem等信息
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:afterRegisterError")
    @PostMapping("/afterRegisterError")
    @ResponseBody
    public AjaxResult afterRegisterError(PaeaAuAuditItem paeaAuAuditItem) {
        String itemId = paeaAuAuditItem.getId();
        String batchId = paeaAuAuditItem.getBatchId();
        PaeaAuBatch batch = paeaAuBatchService.selectPaeaAuBatchById(batchId);
        PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(itemId);
        if(item != null){
            item.setState(AuditItemState.HAVE_ERROR.getCode()); // 2 督办
            item.setAuditorId(getLoginName());
            item.setAuitDate(new Date(System.currentTimeMillis()));
            paeaAuAuditItemService.updatePaeaAuAuditItem(item);
        }
        if(batch != null){
            paeaAuAuditItemService.updateBatchCounts(batch);
        }
        return AjaxResult.success();
    }

    /**
     * 创建督办前验证是否 已被督办过
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:checkRegisterError")
    @PostMapping("/checkRegisterError")
    @ResponseBody
    public AjaxResult checkRegisterError(PaeaAuAuditItem paeaAuAuditItem) {
        String dsTradeId = paeaAuAuditItem.getDsTradeId();
        String erTypeId = "4000";//重点监控
        List<PaeaErError> paeaErErrorList = paeaErErrorService.list(new QueryWrapper<PaeaErError>()
        .eq(dsTradeId !=null && dsTradeId != "","trade_id",dsTradeId)
        .eq(erTypeId != null && erTypeId != "","err_type_id",erTypeId));
        if(paeaErErrorList.size()>0){
            return AjaxResult.warn("该流水已经登记过重点监控风险");
        }
        return AjaxResult.success("该流水可以登记风险");
    }

    /**
     * 新增督办查询
     */
    @GetMapping("/registerErrorPre")
    public String registerErrorPre(PaeaErError paeaErError,ModelMap mmap)
    {
        //TODO 修改整改日期
        paeaErError.setCorrectLimitDate(new java.sql.Date(System.currentTimeMillis()));
        mmap.put("paeaErError",paeaErError);
        return prefix + "/registerErrorWithTrade";
    }

    /**
     * 风险查询界面
     */
    @RequestMapping("/registerErrorView")
    public String registerErrorView(ModelMap mmap, @RequestParam(name = "tradeId") String tradeId) {
        mmap.put("tradeId",tradeId);
        return prefix + "/registerErrorView";
    }

    /**
     * 风险查询界面填充数据
     * @param paeaErError
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:registerErrorTable")
    @PostMapping("/registerErrorTable")
    @ResponseBody
    public TableDataInfo registerErrorTable(PaeaErError paeaErError){
        String tradeId = paeaErError.getTradeId();
        startPage();
        // TODO 修改风险回复状态：未回复0、超期未回复3
        //this.initErrStatus(paeaErErrors);
        List<PaeaErError> paeaErErrorList = paeaErErrorService.list(new QueryWrapper<>(paeaErError)
        .eq(tradeId!=null&&tradeId!="","trade_Id",tradeId));
        return getDataTable(paeaErErrorList);
    }

    /**
     * 删除关联的差错
     * @param
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:deletePaeaErError")
    @PostMapping("/deletePaeaErError")
    @ResponseBody
    public AjaxResult deletePaeaErError(PaeaErError paeaErError) {
        List<String> errIds = new ArrayList<String>();
        String errId = paeaErError.getId();
        String itemId = paeaErError.getAuditItemId();
        errIds.add(errId);
        paeaErErrorService.deletePaeaErError(errIds);
        PaeaAuBatch batch = null;
        //更新 监控批次信息
        PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(itemId);
        if(item != null){
            batch = paeaAuBatchService.selectPaeaAuBatchById(item.getBatchId());
            item.setState(AuditItemState.UN_AUDIT.getCode()); // 1 未监控
            item.setAuditorId("");
            item.setAuitDate(null);
            paeaAuAuditItemService.updatePaeaAuAuditItem(item);
        }
        if(batch != null){
            paeaAuAuditItemService.updateBatchCounts(batch);
        }
        return AjaxResult.success();
    }

    /**
     * 取消监控（正常监控下的）
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:cancelAuditItem")
    @PostMapping("/cancelAuditItem")
    @ResponseBody
    public AjaxResult cancelAuditItem(PaeaAuAuditItem paeaAuAuditItem) {
        String itemId = paeaAuAuditItem.getId();
        String batchId = paeaAuAuditItem.getBatchId();
        //更新 监控批次信息
        PaeaAuAuditItem item = paeaAuAuditItemService.selectPaeaAuAuditItemById(itemId);
        PaeaAuBatch batch = paeaAuBatchService.selectPaeaAuBatchById(batchId);
        if(item != null){
            item.setState(AuditItemState.UN_AUDIT.getCode()); // 1 未监控
            item.setAuditorId("");
            item.setAuitDate(null);
            paeaAuAuditItemService.updatePaeaAuAuditItem(item);
        }
        if(batch != null){
            paeaAuAuditItemService.updateBatchCounts(batch);
        }
        return AjaxResult.success();
    }

    /**
     * 查询批次Id
     * @param sdiPaeaArc
     * @returnd
     */
    @RequiresPermissions("oms:paeaAuAuditItem:querySdiPaeaArcId")
    @PostMapping("/querySdiPaeaArcId")
    @ResponseBody
    public AjaxResult querySdiPaeaArcId(SdiPaeaArc sdiPaeaArc) {
        String arcId = "";
        List<SdiPaeaArc> sdiPaeaArcList = sdiPaeaArcService.selectSdiPaeaArcList(sdiPaeaArc);
        if(sdiPaeaArcList!=null && sdiPaeaArcList.size()>0){
            arcId = sdiPaeaArcList.get(0).getId();
        }else{
            return AjaxResult.error("该批次已删除或不存在!");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("arcId",arcId);
        return ajax;
    }

    /**
     * 复核
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:reAuditConfirm")
    @PostMapping("/reAuditConfirm")
    @ResponseBody
    public AjaxResult reAuditConfirm(PaeaAuAuditItem paeaAuAuditItem) {
        String id = paeaAuAuditItem.getId();
        //Date trDate = paeaAuAuditItem.getTrDate();
        String batchId = paeaAuAuditItem.getBatchId();
        //分表查询
        PaeaAuAuditItem item = paeaAuAuditItemService.getById(id);
        if(item!=null){
            if(item.getReAuFlag() != 1){
                return AjaxResult.warn("该监督项已经复核！");
            }
            else{
                item.setReAuFlag(2);												//再监督标识
                item.setReAuDate(new java.sql.Date(System.currentTimeMillis()));	//再监督时间
                item.setReAuOper(getLoginName());								//再监督人
                paeaAuAuditItemService.updatePaeaAuAuditItem(item);             //更新该监督项

                //保存质检流水全量表
                PaeaAuOperAuditor paeaAuOperAuditor = new PaeaAuOperAuditor();
                paeaAuOperAuditor.setAuditorId(item.getAuditorId());
                paeaAuOperAuditor.setOrgId(item.getOrgId());
                paeaAuOperAuditor.setTrDate(item.getTrDate());
                paeaAuOperAuditor.setTellerId(item.getTellerId());
                paeaAuOperAuditor.setTxnSeq(item.getTxnSeq());
                paeaAuOperAuditor.setAuitDate(item.getAuitDate());
                paeaAuOperAuditor.setReAuOper(item.getReAuOper());
                paeaAuOperAuditor.setReAuDate(item.getReAuDate());
                paeaAuOperAuditor.setAuTypeId(item.getAuTypeId());
                paeaAuOperAuditor.setStatus("0");
                paeaAuOperAuditorService.save(paeaAuOperAuditor);
                //servicePaeaReportBusinessImpl.savePaeaReportBusiness(smUser,new java.util.Date(), 0, 0, 1, 0, 0, 0, 0, 0);	//增加操作日志
                PaeaAuBatch batch = paeaAuBatchService.getById(batchId);
                //分表查询
                List<PaeaAuAuditItem> paeaAuAuditItemList = paeaAuAuditItemService.list(new QueryWrapper<PaeaAuAuditItem>()
                .eq(batchId !=null,"batch_id",batchId)
                .eq("re_au_flag",1)
                .orderByAsc("vch_no"));
                if(paeaAuAuditItemList.size() == 0){
                    batch.setState(2);   //已复核
                    int fsNum =batch.getFsNum()==null?0:batch.getFsNum();
                    fsNum ++;
                    batch.setFsNum(fsNum);
                    batch.setReAuditor(getLoginName());
                    paeaAuBatchService.updatePaeaAuBatch(batch);
                    AjaxResult ajax = AjaxResult.success("该批次已复核完毕!");
                    ajax.put("returnFlag","2");
                    return ajax;
                }else{
                    if(null == batch.getState()){
                        batch.setState(1);   //部分复核
                        paeaAuBatchService.updatePaeaAuBatch(batch);
                    }
                    int fsNum =batch.getFsNum()==null?0:batch.getFsNum();
                    fsNum ++;
                    batch.setFsNum(fsNum);
                    paeaAuBatchService.updatePaeaAuBatch(batch);
                }
                AjaxResult ajax = AjaxResult.success();
                ajax.put("returnFlag","1");
                return ajax;
            }
        }
        return AjaxResult.error("复核错误，请联系管理员！");
    }

    /**
     * 判断是否已复核
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:isReAudit")
    @PostMapping("/isReAudit")
    @ResponseBody
    public AjaxResult isReAudit(PaeaAuAuditItem paeaAuAuditItem) {

        String itemId = paeaAuAuditItem.getId();
        Date trDate = paeaAuAuditItem.getTrDate();
        //分表查询
        PaeaAuAuditItem item = paeaAuAuditItemService.getById(itemId);
        if(item!=null){
            if(item.getReAuFlag() != 1){
                return AjaxResult.warn("该监督项已经复核！");
            }
            else{
                return AjaxResult.success();
            }
        }
        return AjaxResult.success();
    }

    /**
     * 重点监控生成督办信息后会写监控数据中的监控人，监控状态，监控日期
     * @param paeaAuAuditItem
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:registerErrorWithFlow")
    @PostMapping("/registerErrorWithFlow")
    @ResponseBody
    public AjaxResult registerErrorWithFlow(PaeaAuAuditItem paeaAuAuditItem) {
        String itemId = paeaAuAuditItem.getId();
        java.util.Date trDate = paeaAuAuditItem.getTrDate();
        String batchId = paeaAuAuditItem.getBatchId();
        //分表查询
        PaeaAuAuditItem item = paeaAuAuditItemService.getById(itemId);
        PaeaAuBatch batch  = paeaAuBatchService.getById(batchId);
        //得到交易Id 差错ID
        String dsTradeId=item.getDsTradeId();
        List<PaeaErError> paeaErErrorList = paeaErErrorService.list(new QueryWrapper<PaeaErError>()
        .eq("err_source","50")
        .eq("trade_id",dsTradeId)
        .orderByAsc("create_time"));
        String errorId="";
        if(paeaErErrorList.size()>0){
            errorId=paeaErErrorList.get(0).getId();
        }
        //end
        try {
            //增加处理 再监督时创建督办后，按以复核处理
            item.setState(AuditItemState.HAVE_ERROR.getCode()); // 2
            item.setReAuFlag(2);                 //已复核
            item.setReAuOper(getLoginName());
            item.setReAuDate(new Date(System.currentTimeMillis()));
            //保存质检流水全量表
            PaeaAuOperAuditor paeaAuOperAuditor = new PaeaAuOperAuditor();
            paeaAuOperAuditor.setAuditorId(item.getAuditorId());
            paeaAuOperAuditor.setOrgId(item.getOrgId());
            paeaAuOperAuditor.setTrDate(item.getTrDate());
            paeaAuOperAuditor.setTellerId(item.getTellerId());
            paeaAuOperAuditor.setTxnSeq(item.getTxnSeq());
            paeaAuOperAuditor.setAuitDate(item.getAuitDate());
            paeaAuOperAuditor.setReAuOper(item.getReAuOper());
            paeaAuOperAuditor.setReAuDate(item.getReAuDate());
            paeaAuOperAuditor.setAuTypeId(item.getAuTypeId());
            if(!errorId.equals("")){
                paeaAuOperAuditor.setStatus("1");
            }else{
                paeaAuOperAuditor.setStatus("0");
            }
            paeaAuOperAuditor.setErrorId(errorId);
            paeaAuAuditItemService.updatePaeaAuAuditItem(item);
            paeaAuOperAuditorService.save(paeaAuOperAuditor);
            //end
            //servicePaeaReportBusinessImpl.savePaeaReportBusiness(smUser,new java.util.Date(), 0, 0, 1, 0, 0, 0, 0, 0);	//增加操作日志
            //分表查询
            List<PaeaAuAuditItem> paeaAuAuditItemList = paeaAuAuditItemService.list(new QueryWrapper<PaeaAuAuditItem>()
                    .eq(batchId !=null,"batch_id",batchId)
                    .eq("re_au_flag",1)
                    .orderByAsc("vch_no"));
            if(paeaAuAuditItemList.size() == 0){
                int fsNum =batch.getFsNum()==null?0:batch.getFsNum();
                fsNum ++;
                batch.setFsNum(fsNum);
                batch.setReAuditor(getLoginName());
                batch.setState(2);   //已复核
                paeaAuBatchService.updatePaeaAuBatch(batch);
                AjaxResult ajax =  AjaxResult.success("该批次已复核完毕！");
                ajax.put("reflag","2");
                return ajax;
            }
            else{
                if(null == batch.getState()){
                    batch.setState(1);   //部分复核
                    paeaAuBatchService.updatePaeaAuBatch(batch);
                }
                PaeaAuBatch batch1 = paeaAuBatchService.getById(item.getBatchId());
                int fsNum = batch1.getFsNum()==null?0:batch1.getFsNum();
                fsNum++;
                batch1.setFsNum(fsNum);
                paeaAuBatchService.updatePaeaAuBatch(batch1);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
        AjaxResult ajax =  AjaxResult.success();
        ajax.put("reflag","1");
        return ajax;
    }

    /**
     * 下一个批次
     * @param paeaAuBatchRequest
     * @return
     */
    @RequiresPermissions("oms:paeaAuAuditItem:toNextReAuditItemInfo")
    @PostMapping("/toNextReAuditItemInfo")
    @ResponseBody
    public AjaxResult toNextReAuditItemInfo(PaeaAuBatchRequest paeaAuBatchRequest) {
        //此处根据用户查询条件确定下一批次数据
        Date beginDate = paeaAuBatchRequest.getBeginDate();
        Date endDate = paeaAuBatchRequest.getEndDate();
        String orgId = paeaAuBatchRequest.getOrgId();
        String auditorId = paeaAuBatchRequest.getAuditorId();
        int batchType = paeaAuBatchRequest.getBatchType()==null?0:paeaAuBatchRequest.getBatchType();
        int state = paeaAuBatchRequest.getState()==null?0:paeaAuBatchRequest.getState();

        try {
            //TODO  监督任务分配 暂未做
            //监督任务分配方式
//            List<PaeaAuTaskAssignManner> taskAssignManners = paeaAuTaskAssignMannerService.list();
//            String manner = "";
//            if(null != taskAssignManners){
//                manner = taskAssignManners.get(0).getManner();
//            }

            List<PaeaAuBatch> paeaAuBatchList =  paeaAuBatchService.queryReAuditBatchList(orgId,batchType,state,beginDate,endDate,getLoginName(),auditorId);
            String reInfo = "";
            if(null != paeaAuBatchList && paeaAuBatchList.size() > 0){
                PaeaAuBatch batch = paeaAuBatchList.get(0);
                //分表查询
//                List<PaeaAuAuditItem> auditItems = paeaAuAuditItemService.queryItemForBatchId(batch.getId());
//                if(null != auditItems && auditItems.size() > 0){
//                    PaeaAuAuditItem auditItem = auditItems.get(0);
//                    reInfo = auditItem.getId() + "@@" + DateUtils.parseDateToStr("yyyyMMdd",auditItem.getTrDate())  + "@@" + batch.getId();
//                    AjaxResult ajax = AjaxResult.success();
//                    ajax.put("reInfo",reInfo);
//                }
                AjaxResult ajax = AjaxResult.success();
                ajax.put("nextBatchId",batch.getId());
                return ajax;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

        return AjaxResult.warn("已无符合条件的重点监督批次，请重新查询再监督批次！");
    }

    /**
     * 登记缺票
     * @param id
     * @return
     */
    @PostMapping("/lostVoucherRegister")
    @ResponseBody
    public AjaxResult lostVoucherRegister(String id){
        this.paeaAuAuditItemService.lostVoucherRegister(id);
        return AjaxResult.success();
    }
}

